#include <iostream>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/RollingFileAppender.hh>
#include <log4cpp/Category.hh>
using std::cout;
using std::endl;
using namespace log4cpp;
//希望定制一个日志系统：有三个不同的输出目的地
//分别是控制台、文件、滚动文件
//加上不同的日志记录器


void test(){
    PatternLayout * pl1 = new PatternLayout();
    pl1->setConversionPattern("%d %c [%p] %m%n");
    PatternLayout * pl2 = new PatternLayout();
    pl2->setConversionPattern("%d %c [%p] %m%n");
    PatternLayout * pl3 = new PatternLayout();
    pl3->setConversionPattern("%d %c [%p] %m%n");
    OstreamAppender * ap1 = new OstreamAppender("console", &cout);
    ap1->setLayout(pl1);
    FileAppender * ap2 = new FileAppender("file", "wd.log");
    ap2->setLayout(pl2);
    RollingFileAppender * ap3 = new RollingFileAppender("rolling", 
                                                        "rolling.log", 1024 * 1024, 9);
    ap3->setLayout(pl3);

    //设置category
    //使用getInstance函数可以得到叶子记录器
    //但是此时依然会创建一个隐形的root根记录器
    Category & root = Category::getRoot();
    root.setPriority(Priority::INFO);
    root.addAppender(ap1);
    root.addAppender(ap2);
    root.addAppender(ap3);
    Category & product = root.getInstance("product");
    Category & order = root.getInstance("order");

    //不可以将product的category和order的category关联同一个appender
    //否则依然会出现段错误
    //应该是每个category关联自己独有的一份appender
    //
    //order.addAppender(ap1);
    //order.addAppender(ap2);
    //order.addAppender(ap3);

    for(int i = 0; i < 100; ++i){
        product.debug("product debug");
        product.info("product info");
        product.alert("product alert");
        order.debug("product debug");
        order.info("product info");
        order.alert("product alert");
    }

    //回收资源
    //为了规范起见
    Category::shutdown();
}

int main()
{
    test();
    return 0;
}

